//声明地图对象变量
var vtxMap = {
	gis: {},//地图方法功能的对象
	idsForGraphicId:[],//地图上所有图元的集合
	defaultPoint: '/resources/images/defaultMarker.png',
	//简单初始化地图
	init: function (id) {
		//初始化地图对象
		vtxMap.gis = new VortexBMap();
		//初始化地图
		vtxMap.gis.createMap(id?id:'map');
		if(typeof mapConfig != 'undefined' && mapConfig.longitude && mapConfig.latitude){
			vtxMap.setCenter([mapConfig.longitude,mapConfig.latitude]);
		}else{
			/*=======【黄腾】【2017-10-20】【浏览器获取当前坐标点】========*/
			var geolocation = new BMap.Geolocation();
			geolocation.getCurrentPosition(function(p){
				if(this.getStatus() == BMAP_STATUS_SUCCESS){
					vtxMap.setCenter([p.point.lng,p.point.lat]);
					// var geoc = new BMap.Geocoder();
					// var pt = new BMap.Point(p.point.lng,p.point.lat);
					// geoc.getLocation(pt, function(rs){//解析格式：城市，区县，街道
					// 	var addComp = rs.addressComponents;
					// });
				}
			},{enableHighAccuracy:true});
			/*=======【黄腾】【2017-10-20】【浏览器获取当前坐标点】========*/
		}
		if(typeof mapConfig != 'undefined' && mapConfig.zoom){
			vtxMap.setZoom(mapConfig.zoom);
		}
		if(typeof isMapNoScroll == 'undefined' || isMapNoScroll == 0){
			vtxMap.gis.map.enableScrollWheelZoom(); // 开启鼠标滚轮缩放
		}

		//点击地图世界
		vtxMap.gis.bind('clickMap', function(param) {
			if(!!VtxGps.historyLinelg[0]){
				var x = parseFloat(param.eventX);
				var y = parseFloat(param.eventY);
				findNearest(VtxGps.historyLinelg, x, y);
			}
			if(mapToReact.drawPoint){
				mapToReact.drawPoint(param);
			}
		});
		//右击地图
		vtxMap.gis.bind('onRightClick', function(param) {
			if(mapToReact.rightClick){
				mapToReact.rightClick(param);
			}
			return false;
		});
		//绘制结束
		vtxMap.gis.bind('drawEnd',function(param){
			if(mapToReact.drawLineEnd){
				mapToReact.drawLineEnd(param);
			}
			return false;
		});
		//修改线后
		vtxMap.gis.bind('graphicVortexChange',function(param){
			if (mapToReact.drawLineChange) {
				mapToReact.drawLineChange(param);
			}
			return false;
		});
		//编辑移动
		vtxMap.gis.bind('editGraphicMoveEnd',function(param){
			if(param.attributes.other && param.attributes.other.type){
				if(mapToReact.mousePoint){
					mapToReact.mousePoint(param.attributes.other);
				}
			}
			return false;
		})
		//点击事件
		vtxMap.gis.bind('clickGraphic', function(param) {
			//显示详细
			if(param.attributes.other && param.attributes.other.type){
				if(mapToReact.mouseClick){
					mapToReact.mouseClick(param.attributes.other);
				}
			}
			return false;
		});
		//鼠标滑动过事件
		vtxMap.gis.bind('mouseOverGraphic', function(param) {
			if(param.attributes.other && param.attributes.other.type){
				if(mapToReact.mouseOver){
					mapToReact.mouseOver(param.attributes.other);
				}
			}
		});
		//鼠标离开时
		vtxMap.gis.bind('mouseOutGraphic', function(param) {
			if(param.attributes.other && param.attributes.other.type){
				vtxMap.gis.closeInfoWindow();
				if(mapToReact.mouseOut){
					mapToReact.mouseOut(param.attributes.other);
				}
			}
			return false;
		});
		vtxMap.cluster();
	},
	//清空地图所有图元
	clearAll: function () {
		vtxMap.gis.clear();
		vtxMap.idsForGraphicId = [];
	},
	//设置地图比例尺
	setZoom: function (i) {
		vtxMap.gis.setZoom(i);
	},
	//设置地图中心位置 lng/经度  lat/纬度
	setCenter:function (gt) {
		vtxMap.gis.setCenter(gt[0],gt[1]);
	},
	//显示或隐藏图元  图元id visible true显示/false隐藏
	setGraphic: function (id,visible) {
		vtxMap.gis.setGraphicVisibleById(id, visible);
	},
	//设置鼠标样式 默认改成pointer
	setCursor: function (url) {
		vtxMap.gis.setCursor(url);
	},
	//获取当前地图中心点 返回:[lng,lat]
	getNowCenter: function () {
		return vtxMap.gis.getNowCenter();
	},
	//获取面积
	getArea: function (id) {
		return vtxMap.gis.calculateArea(vtxMap.getGraphic(id));
	},
	//获取图元数据
	/*
		attributes 初始化数据(即添加点时的初始数据)
		geometryType:point/polyline/polygon/circle
		mapLayer
			点 rc / 线 Ac / 面 zc / 圆 Bc
		geometry 格式化数据
			点 {type:point,x:lng,y:lat}
			线 {type:polyline,paths:[[lng,lat],[lng,lat]]}
			面 {type:polygon,rings:[[lng,lat],[lng,lat],[lng,lat]]}
			圆 {type:circle,x:lng,y:lat,radius:xxx}
	*/
	getGraphic: function (id) {
		return vtxMap.gis.getGraphic(id);
	},
	//删除图元
	removeGraphic: function (id) {
		var graphic = vtxMap.gis.getGraphic(id);
		vtxMap.gis.removeGraphic(graphic);
		if(vtxMap.idsForGraphicId.indexOf(id) != -1){
			vtxMap.idsForGraphicId.splice(vtxMap.idsForGraphicId.indexOf(id),1);
		}
	},
	//聚合地图图元(arg为空时聚合全部点)
	cluster: function (arg) {
		var ary = vtxMap.idsForGraphicId;
		if(Object.prototype.toString.apply(arg) === '[Object Array]')
			ary = !arg[0]?ary:arg;
		vtxMap.gis.cluster(ary);
	},
	//将制定图元展示在视野内 (强制改变地图中心位置)
	/*
		参数arg格式如下1,2
		1.string   格式如:'1,a,2,3,4'
		2.数组 ['1','2']
	*/
	setFitview: function (arg) {
		if(typeof(arg) === 'string'){
			vtxMap.gis.setFitview(arg.split(','));
		}else if(typeof(arg) === 'object' && !!arg.length){
			vtxMap.gis.setFitview(arg);
		}else{
			vtxMap.gis.setFitview(vtxMap.idsForGraphicId);
		}
	},
	//地图加点(支持多个和单个)
	/*
		参数 [{
				id : 唯一id,
				longitude : 经度,
				latitude : 纬度,
				infoWindow : 是否有弹出 默认true,
				url : 图标url,
				markerContent: 点样式,//跟url冲突,存在markerContent时,url不展示
				config : {
					width:图标宽 默认30,
					height:图标高 默认15
				}
			}]
	 */
	addPoint:function(arg) {
		var t = vtxMap;
		arg.map(function (item,index) {
			if(t.isRepetition(item.id)){
				t.gis.addPoint({
					id : item.id,
					longitude : item.longitude,
					latitude : item.latitude,
					infoWindow : item.infoWindow || true,
					url : item.url || '',
					markerContent: item.markerContent || '',
					canShowLabel: item.canShowLabel || false,
					config : !item.config?{}:
					{
						labelContent: item.config.labelContent || '',
						labelPixelX: -36,
						labelPixelY: 34,
						width : item.config.width || 30,
						height : item.config.height || 30,
						markerContentX : item.config.markerContentX || -50,
						markerContentY : item.config.markerContentY || -50
					},
					other:item
				});
			}
		});
	},
	//更新地图点数据(支持多个和单个) {暂时有不用,未完善}

	/*	参数 [{
				id : 唯一id,
				longitude : 经度,
				latitude : 纬度,
				infoWindow : 是否有弹出 默认true,
				url : 图标url,
				markerContent: 点样式,//跟url冲突,存在markerContent时,url不展示
				config : {
					width:图标宽 默认30,
					height:图标高 默认15,
					labelContent: 下标文字,
					labelPixelX: x轴下标偏移量,
					labelPixelY: y轴下标偏移量,
				}
			}]
	*/
	updatePoint: function (arg) {
		var t = vtxMap;
		arg.map(function (item,index) {
			t.gis.updatePoint({
				id : item.id,
				longitude : item.longitude,
				latitude : item.latitude,
				infoWindow : item.infoWindow || true,
				url : item.url || '',
				markerContent: item.markerContent || '',
				canShowLabel: item.canShowLabel || false,
				other:item.other,
				config : !item.config?{}:
				{
					width : item.config.width || 30,
					height : item.config.height || 15,
					labelContent: item.config.labelContent || '',
					labelPixelX: item.config.labelPixelX || 0,
					labelPixelY: item.config.labelPixelY || 0,
					markerContentX : item.config.markerContentX || -50,
					markerContentY : item.config.markerContentY || -50,
					deg:item.config.deg || 0
				}
			});
		});
	},
	//地图加线(支持多个和单个) paths:必须含2个点,即含有2个有经纬度的数组
	/*
		参数 [{
				id : 唯一id,
				paths:[[经度,纬度],[经度,纬度],[经度,纬度]],
				infoWindow : 是否有弹出 默认true,
				config : {
					lineType : 线类型（实线solid，虚线dashed）默认实线solid
					lineWidth :  线宽, 默认5
					color : 线颜色, 默认[]
					pellucidity : 线透明度(0-1), 默认1
				}
			}]
	 */
	addLine: function (arg) {
		var t = vtxMap;
		arg.map(function (item,index) {
			if(t.isRepetition(item.id)){
				t.gis.addLine({
					id : item.id,
					paths : [item.paths],
					infoWindow : item.infoWindow || true,
					config : !item.config?{}:
					{
						lineType : item.config.lineType || 'solid',
						lineWidth : item.config.lineWidth || 5,
						color : item.config.color || [],
						pellucidity : item.config.pellucidity || 1
					},
					other:item
				});
			}
		});
	},
	//地图加面(支持多个和单个) rings:必须含3个点,即含有3个有经纬度的数组
	/*
		参数 [{
				id : 唯一id,
				rings:[[经度,纬度],[经度,纬度],[经度,纬度]],
				infoWindow : 是否有弹出 默认true,
				config : {
					lineType : 线类型（实线solid，虚线dashed）默认实线solid
					lineWidth :  线宽, 默认5
					color : 填充颜色, 默认[]
					pellucidity : 填充透明度(0-1), 默认1
					lineColor: 线颜色, 默认''
					lineOpacity: 线透明度, 默认1
				}
			}]
	 */
	addPolygon: function (arg) {
		var t = vtxMap;
		arg.map(function (item,index) {
			if(t.isRepetition(item.id)){
				t.gis.addPolygon({
					id : item.id,
					geometryType: item.geometryType,
					rings : [item.rings],
					infoWindow : item.infoWindow || true,
					config : !item.config?{}:
					{
						lineType : item.config.lineType || 'solid',
						lineWidth : item.config.lineWidth || 5,
						color : item.config.color || "",
						pellucidity : item.config.pellucidity || 1,
						lineColor : item.config.lineColor || "",
						lineOpacity : item.config.lineOpacity || 1
					},
					other:item
				});
			}
		});
	},
	//地图加面(支持多个和单个) rings:必须含3个点,即含有3个有经纬度的数组
	/*
		参数 [{
				id : 唯一id,
				longitude : 经度,
				latitude : 纬度,
				infoWindow : 是否有弹出 默认true,
				radius : 半径, 单位是m/米 默认50
				config : {
					lineType : 线类型（实线solid，虚线dashed）默认实线solid
					lineWidth :  线宽, 默认5
					color : 填充颜色, 默认'#FFF'
					pellucidity : 填充透明度(0-1), 默认1
					lineColor: 线颜色, 默认''
					lineOpacity: 线透明度, 默认1
				}
			}]
	 */
	addCircle: function (arg) {
		var t = vtxMap;
		arg.map(function (item,index) {
			if(t.isRepetition(item.id)){
				t.gis.addCircle({
					id : item.id,
					longitude : item.longitude,
					latitude : item.latitude,
					radius : item.radius || 50,
					infoWindow : item.infoWindow || true,
					config : !item.config?{}:
					{
						lineType : item.config.lineType || 'solid',
						lineWidth : item.config.lineWidth || 5,
						color : item.config.color || "#FFF",
						pellucidity : item.config.pellucidity || 1,
						lineColor : item.config.lineColor || "#FFF",
						lineOpacity : item.config.lineOpacity || 1
					},
					other:item
				});
			}
		});
	},
	//绘制图元
	/*
		参数
		geometryType:point/polyline/polygon/circle  默认point
		parameter 样式 默认null 对象{}写入方式跟add方法一样(对应点线圆面)
		data //初始化数据   默认{id:'add'}
	*/
	draw: function (obj) {
		obj = obj || {};
		var t = vtxMap;
		if((obj.data && t.isRepetition(obj.data.id)) || (!obj.data && t.isRepetition('graphic'))){
			vtxMap.gis.draw(
				obj.geometryType || 'point',
				obj.parameter || {url:t.defaultPoint},
				obj.data || {
					id : "graphic"
				}
			);
		}
	},
	//测距
	vtxRangingTool: function (callback) {
		vtxMap.gis.vtxRangingTool(function(obj){
			callback(obj);
		});
	},
	//添加点线圆面时判断对应的id是否与已有的id重复,重复不添加
	isRepetition: function (id){
		var t = vtxMap;
		for(var i = 0 ; i < t.idsForGraphicId.length ; i++){
			if(t.idsForGraphicId[i] === id){
				console.error('warn: Add failure because id(' + id + ') existed!');
				return false;
			}
		}
		t.idsForGraphicId.push(id);
		return true;
	}
};

var VtxGps = {
	allCarId: [],//所有地图上的车点id
	carlngWithTraced: [],//跟踪车辆的经纬度(点)
	carlngsWithTraced: [[]],//跟踪车辆的经纬度集合(线)
	gasStationIds: [],//加油站所有id
	pitIds: [],//维修厂所有id
	//缓存实时轨迹全部车辆和跟踪车辆
	allCarPoints: [],
	idWithTraced: [],
	//缓存的关注区
	showPolygonIds: [],
	showCircleIds: [],
	showPolylineIds: [],
	showPointIds: [],
	showRectangleIds: [],
	//所有历史轨迹路线
	historyLinelg: [],
	//历史轨迹中路线等id
	historyIds: [],
	//添加所有加油站
	addGasStation: function (ids) {
		var ary = [];
		ids.map(function (item,index) {
			ary.push({
				id: 'gasStation'+index,
				longitude: item.longitude,
				latitude: item.latitude,
				type: 'gasStation',
				url: '/resources/images/carGasStation.png',
				other: item.other,
				config:{}
			});
			VtxGps.gasStationIds.push('gasStation'+index);
		});
		vtxMap.addPoint(ary);
		vtxMap.setFitview();
	},
	//删除所有加油站
	deleteGasStation: function () {
		if(this.gasStationIds[0]){
			VtxGps.gasStationIds.map(function (item,index) {
				vtxMap.removeGraphic(item);
			});
			VtxGps.gasStationIds = [];
		}
	},
	//添加所有维修厂
	addPit: function (ids) {
		var ary = [];
		ids.map(function (item,index) {
			ary.push({
				id: 'pit'+index,
				longitude: item.longitude,
				latitude: item.latitude,
				type: 'pit',
				url: '/resources/images/carShops.png',
				other: item.other,
				config:{}
			});
			VtxGps.pitIds.push('pit'+index);
		});
		vtxMap.addPoint(ary);
		vtxMap.setFitview();
	},
	//删除所有维修厂
	deletePit: function () {
		if(this.pitIds[0]){
			VtxGps.pitIds.map(function (item,index) {
				vtxMap.removeGraphic(item);
			});
			VtxGps.pitIds = [];
		}
	},
	//临时加点更新
	updatePoint: function (id,text) {
		vtxMap.updatePoint([{
  			id:id,
			canShowLabel: true,
  			config:{
  				labelContent: text || '',
  				labelPixelX: 0,
				labelPixelY: 32
  			}
  		}]);
	},
	//临时加点
	drawPoint: function () {
		var id = 'p' + new Date().getTime();
		vtxMap.draw({
			geometryType:'point',
			parameter:{
				url: vtxMap.defaultPoint
			},
			data: {
				id: id
			}
		});
		vtxMap.gis.bind('drawEnd', function(src) {
			// VtxGps.updatePoint(id);
			mapToReact.openBox(id);
		});
	},
	//测距功能
	vtxRangingTool: function () {
		vtxMap.vtxRangingTool(function(item){
		});
	},
	//实时页面,添加所有点
	initAllCarPoints: function () {
		//先清空所有点
		VtxGps.allCarId.map(function (item,index) {
			if(vtxMap.getGraphic(item)){
				vtxMap.removeGraphic(item);
			}
		});
		//清空点id
		VtxGps.allCarId = [];
		//初始化所有点数据
		var ary = positionArr.map(function (item,index) {
			//记录所有点id
			VtxGps.allCarId.push(item.id);
			return {
				id: item.id,
				longitude: item.longitudeDone,
				latitude: item.latitudeDone,
				url: getGisStatus(item.carStatus,item.direction),
				canShowLabel: true,
				carId:item.carId,
	  			config:{
	  				labelContent: item.carCode || '',
	  				labelPixelX: 0,
					labelPixelY: 32
	  			}
			}
		});
		//添加所有点
		vtxMap.addPoint(ary);
		vtxMap.setFitview(this.allCarId);
	},
	//清空实时界面车辆
	removeCarPoints: function (txt) {
		var ary = [];
		if(txt == 'all'){
			ary = VtxGps.allCarId;
		}else{
			ary = VtxGps.idWithTraced;
		}
		ary.map(function (item,index) {
			if(vtxMap.getGraphic(item)){
				vtxMap.removeGraphic(item);
			}
		});
	},
	//跟踪车辆
	traceCar: function (isStart,lg,lngs) {
		//处理数据
		VtxGps.carlngWithTraced = [{
			id: lg.id,
			longitude: lg.longitudeDone,
			latitude: lg.latitudeDone,
			url: getGisStatus(lg.carStatus,lg.direction),
			canShowLabel: true,
  			config:{
  				labelContent: lg.carCode || '',
  				labelPixelX: 0,
				labelPixelY: 32
			}
		}];
		VtxGps.carlngsWithTraced.push(lngs);
		//根据数据更新点线
		VtxGps.updateLineBycar();
	},
	//刷新跟踪车辆路线
	updateLineBycar: function () {
		//清除线
		if(vtxMap.getGraphic((VtxGps.carlngWithTraced.id + '_line'))){
			vtxMap.removeGraphic((VtxGps.carlngWithTraced.id + '_line'));
		}
		//加线
		vtxMap.addLine([{
			id: (VtxGps.carlngWithTraced.id + '_line'),
			paths: VtxGps.carlngsWithTraced
		}]);
		//加点和更新点
		if(vtxMap.getGraphic(VtxGps.carlngWithTraced[0].id)){
			vtxMap.updatePoint(VtxGps.carlngWithTraced);
		}else{
			vtxMap.addPoint(VtxGps.carlngWithTraced);
		}
		VtxGps.idWithTraced.push(VtxGps.carlngWithTraced[0].id);
		vtxMap.setFitview(VtxGps.carlngWithTraced[0].id);
		vtxMap.setCenter([VtxGps.carlngWithTraced[0].longitude,VtxGps.carlngWithTraced[0].latitude]);
	},
	//新增关注区
	drwaFocusArea: function (type) {
		var id = type + new Date().getTime();
		switch(type){
			case 'point':
				vtxMap.draw({
					geometryType: type,
					parameter: {
						url: vtxMap.defaultPoint
					},
					data: {
						id: id
					}
				});
				vtxMap.gis.bind('drawEnd', function(src) {
					var obj = {
						id: src.attributes.id,
						longitude: src.geometry.x,
						latitude: src.geometry.y
					}
					mapToReact.openAddCareBox(obj);
				});
				break;
			case 'polyline':
				vtxMap.draw({
					geometryType: type,
					parameter: {},
					data: {
						id: id
					}
				});
				vtxMap.gis.bind('drawEnd', function(src) {
					var obj = {
						id: src.attributes.id,
						paths: src.geometry.paths[0],
						length: vtxMap.gis.calculateDistance(src.geometry.paths[0])
					}
					mapToReact.openAddCareBox(obj);
				});
				break;
			case 'polygon':
				vtxMap.draw({
					geometryType: type,
					parameter: {opacity:0},
					data: {
						id: id
					}
				});
				vtxMap.gis.bind('drawEnd', function(src) {
					var obj = {
						id: src.attributes.id,
						rings: src.geometry.rings[0],
						area: vtxMap.getArea(src.attributes.id)
					}
					mapToReact.openAddCareBox(obj);
				});
				break;
			case 'circle':
				vtxMap.draw({
					geometryType: type,
					parameter: {},
					data: {
						id: id
					}
				});
				vtxMap.gis.bind('drawEnd', function(src) {
					var obj = {
						id: src.attributes.id,
						longitude: src.geometry.x,
						latitude: src.geometry.y,
						radius: src.geometry.radius,
						area: vtxMap.getArea(src.attributes.id)
					}
					mapToReact.openAddCareBox(obj);
				});
				break;
			case 'rectangle':
				vtxMap.draw({
					geometryType: type,
					parameter: {},
					data: {
						id: id
					}
				});
				vtxMap.gis.bind('drawEnd', function(src) {
					var obj = {
						id: src.attributes.id,
						rings: src.geometry.rings[0],
						area: vtxMap.getArea(src.attributes.id)
					}
					mapToReact.openAddCareBox(obj);
				});
				break;
		}
	},
	//切换关注区id
	changeIdbyFocusArea: function (id,newId) {
		//获取原先数据
		var param = vtxMap.getGraphic(id);
		//删除图元
		vtxMap.removeGraphic(id);
		//刷新图元
		switch(param.geometryType){
			case 'point':
				vtxMap.addPoint([{
					id: newId,
					longitude: param.geometry.x,
					latitude: param.geometry.y,
					other: {
						type: 'focusArea'
					}
				}]);
				break;
			case 'polyline':
				vtxMap.addLine([{
					id: newId,
					paths: param.geometry.paths[0],
					other: {
						type: 'focusArea'
					}
				}]);
				break;
			case 'polygon':
				vtxMap.addPolygon([{
					id: newId,
					geometryType: param.geometryType,
					rings: param.geometry.rings[0],
					other: {
						type: 'focusArea'
					}
				}]);
				break;
			case 'circle':
				vtxMap.addCircle([{
					id: newId,
					longitude: param.geometry.longitude,
					latitude: param.geometry.latitude,
					radius:  param.geometry.radius,
					other: {
						type: 'focusArea'
					}
				}]);
				break;
			case 'rectangle':
				vtxMap.addPolygon([{
					id: newId,
					geometryType: param.geometryType,
					rings: param.geometry.rings[0],
					other: {
						type: 'focusArea'
					}
				}]);
				break;
		}
	},
	//查看关注区
	showFocusArea: function (obj) {
		switch(obj.type){
			case 'point':
				obj.param.other={
					type: 'focusArea'
				}
				vtxMap.addPoint([obj.param]);
				VtxGps.showPointIds.push(obj.param.id);
				break;
			case 'polyline':
				obj.param.other={
					type: 'focusArea'
				}
				vtxMap.addLine([obj.param]);
				VtxGps.showPolylineIds.push(obj.param.id);
				break;
			case 'polygon':
				obj.param.other={
					type: 'focusArea'
				}
				vtxMap.addPolygon([obj.param]);
				VtxGps.showPolygonIds.push(obj.param.id);
				break;
			case 'circle':
				obj.param.other={
					type: 'focusArea'
				}
				vtxMap.addCircle([obj.param]);
				VtxGps.showCircleIds.push(obj.param.id);
				break;
			case 'rectangle':
				obj.param.other={
					type: 'focusArea'
				}
				vtxMap.addPolygon([obj.param]);
				VtxGps.showRectangleIds.push(obj.param.id);
				break;
		}
		vtxMap.setFitview();
	},
	//删除对应关注区
	deleteShowFocusArea: function (type) {
		switch(type){
			case 'point':
				VtxGps.showPointIds.map(function (item,index) {
					if(vtxMap.getGraphic(item)){
						vtxMap.removeGraphic(item);
					}
				});
				VtxGps.showPointIds = [];
				break;
			case 'polyline':
				VtxGps.showPolylineIds.map(function (item,index) {
					if(vtxMap.getGraphic(item)){
						vtxMap.removeGraphic(item);
					}
				});
				VtxGps.showPolylineIds = [];
				break;
			case 'polygon':
				VtxGps.showPolygonIds.map(function (item,index) {
					if(vtxMap.getGraphic(item)){
						vtxMap.removeGraphic(item);
					}
				});
				break;
			case 'circle':
				VtxGps.showCircleIds.map(function (item,index) {
					if(vtxMap.getGraphic(item)){
						vtxMap.removeGraphic(item);
					}
				});
				VtxGps.showCircleIds = [];
				break;
			case 'rectangle':
				VtxGps.showRectangleIds.map(function (item,index) {
					if(vtxMap.getGraphic(item)){
						vtxMap.removeGraphic(item);
					}
				});
				VtxGps.showRectangleIds = [];
				break;
		}
	},
	//清空历史轨迹路线等点线
	clearHistoryGraphic: function () {
		VtxGps.historyIds.map(function (item,index) {
			if(vtxMap.getGraphic(item)){
				vtxMap.removeGraphic(item);
			}
		})
		VtxGps.historyIds = [];
	},
	//历史轨迹路线 和 起始  和  终点  3个点
	historyLine: function (obj) {
		if(vtxMap.getGraphic('attentionPoint')){
			vtxMap.removeGraphic('attentionPoint');
		}
		VtxGps.clearHistoryGraphic();
		vtxMap.addPoint([{
			id: 'start_' + obj[0].id,
			longitude: obj[0].longitudeDone,
			latitude: obj[0].latitudeDone,
			url: '/resources/images/track_start.png',
			config :{
				width: '22px',
    			height: '31px'
			}
		},{
			id: 'end_' + obj[0].id,
			longitude: obj[obj.length -1].longitudeDone,
			latitude: obj[obj.length -1].latitudeDone,
			url: '/resources/images/track_end.png',
			config :{
				width: '22',
    			height: '31'
			}
		}]);
		var paths = obj.map(function (item,index) {
			return [item.longitudeDone,item.latitudeDone];
		})
		vtxMap.addLine([{
			id: 'line_' + obj[0].id,
			paths: paths
		}]);
		VtxGps.historyIds.push('start_' + obj[0].id);
		VtxGps.historyIds.push('end_' + obj[0].id);
		VtxGps.historyIds.push('line_' + obj[0].id);
		VtxGps.historyLinelg = paths;
		vtxMap.setFitview(VtxGps.historyIds);
	},
	//历史轨迹车辆点
	historyCar: function (lg) {
		if(vtxMap.getGraphic('car_history')){
			vtxMap.updatePoint([{
				id: 'car_history',
				longitude: lg.longitudeDone,
				latitude: lg.latitudeDone,
				url: getGisStatus(lg.carStatus,lg.direction),
				canShowLabel: true,
	  			config:{
	  				labelContent: lg.carCode || '',
	  				labelPixelX: 0,
					labelPixelY: 32
				}
			}]);
		}else{
			vtxMap.addPoint([{
				id: 'car_history',
				longitude: lg.longitudeDone,
				latitude: lg.latitudeDone,
				url: getGisStatus(lg.carStatus,lg.direction),
				canShowLabel: true,
	  			config:{
	  				labelContent: lg.carCode || '',
	  				labelPixelX: 0,
					labelPixelY: 32
				}
			}]);
		}
	}

}

// 获取车辆状态码
function getGisStatus(carStatus, direction) {
	var suffix = "01.png";
	if (direction >= 0 && direction <= 22.5 || direction > 337.5
			&& direction <= 360) {
		suffix = "01.png";
	}
	if (direction > 22.5 && direction <= 67.5) {
		suffix = "02.png";
	}
	if (direction > 67.5 && direction <= 112.5) {
		suffix = "03.png";
	}
	if (direction > 112.5 && direction <= 157.5) {
		suffix = "04.png";
	}
	if (direction > 157.5 && direction <= 202.5) {
		suffix = "05.png";
	}
	if (direction > 202.5 && direction <= 247.5) {
		suffix = "06.png";
	}
	if (direction > 247.5 && direction <= 292.5) {
		suffix = "07.png";
	}
	if (direction > 292.5 && direction <= 337.5) {
		suffix = "08.png";
	}
	if ("行驶在线" == carStatus || "点火" == carStatus || carStatus == null) {
		return "/resources/images/onfire/" + suffix;
	}
	if ("停车在线" == carStatus || "熄火" == carStatus) {
		return "/resources/images/stop.png";
	}
	if ("离线" == carStatus) {
		return "/resources/images/offline/" + suffix;
	}
};
var _gisType = 'bMap';
function findNearest(arr, x, y) {
	var errorStandard = 0.001 * vtxMap.gis.getScale();
	if (_gisType == "bMap") {
		errorStandard = 5;
	}
	var numArr = new Array();
	for (var i = 0; i < arr.length; i++) {
		// 下一个要判断的经纬度
		// var pointX = arr[i].longitudeDone;
		var pointX = arr[i][0];
		// var pointY = arr[i].latitudeDone;
		var pointY = arr[i][1];
		if (Math.abs(pointX - x) < errorStandard
				&& Math.abs(pointY - y) < errorStandard) {
			numArr.push(arr[i]);
		}
	}
	if (numArr.length == 0) {
	} else if (numArr.length == 1) {
		if(vtxMap.getGraphic('attentionPoint')){
			vtxMap.removeGraphic('attentionPoint');
		}
		vtxMap.addPoint([{
			id: 'attentionPoint',
			longitude: VtxGps.historyLinelg[0][0],
			latitude: VtxGps.historyLinelg[0][1],
			url: '/resources/images/clickPosition.png',
			config :{
				width: '13',
    			height: '21'
			}
		}])
	} else if (numArr.length > 1) {
		// 最近距离，由于最先不知道具体的，所以设为负数
		var spaceNearest = null;
		// 对所有点判断是否是最短距离的点
		for (var j = 0; j < numArr.length; j++) {
			// 下一个要判断的经纬度
			// var pointX = parseFloat(numArr[j].longitudeDone);
			var pointX = parseFloat(numArr[j][0]);
			// var pointY = parseFloat(numArr[j].latitudeDone);
			var pointY = parseFloat(numArr[j][1]);

			// 当前点和鼠标点击点的距离
			var spaceTwoPoint = getTwoPointDistance(x, y, pointX, pointY);
			// 获取最短距离
			if (!spaceNearest) {
				spaceNearest = spaceTwoPoint;
			}
			if (spaceTwoPoint <= spaceNearest) {
				spaceNearest = spaceTwoPoint;
				nearestPointNumber = j;
			}
		}
		var selectId = numArr[nearestPointNumber].id;
		//获取gps信息 查看对应id点的基本信息
		// getGpsInfoById(selectId);
		//滚动信息列表
		mapToReact.scrollToIndex(nearestPointNumber);
		//加点
		if(vtxMap.getGraphic('attentionPoint')){
			vtxMap.removeGraphic('attentionPoint');
		}
		vtxMap.addPoint([{
			id: 'attentionPoint',
			longitude: VtxGps.historyLinelg[nearestPointNumber][0],
			latitude: VtxGps.historyLinelg[nearestPointNumber][1],
			url: '/resources/images/clickPosition.png',
			config :{
				width: '13',
    			height: '21'
			}
		}])
	}
}
function rad(d) {
	return d * Math.PI / 180.0;
}
var EARTH_RADIUS = 6378.137;
function getTwoPointDistance(lng1, lat1, lng2, lat2) {
	var radLat1 = rad(lat1);
	var radLat2 = rad(lat2);
	var a = radLat1 - radLat2;
	var b = rad(lng1) - rad(lng2);
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
			+ Math.cos(radLat1) * Math.cos(radLat2)
			* Math.pow(Math.sin(b / 2), 2)));
	s = s * EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000;
	return s;
}